package com.yx.service.impl;

import com.yx.dao.PersonMapper;
import com.yx.entity.Person;
import com.yx.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

/**
 * @Classname PersonServiceImpl
 * @Date 2021/05/18 10:58
 * @Created by yx
 * @Description TODO:spring提供的注解 @Autowired 默认使用数据类型进行注入,如果出现2个相同类型,就自动切换使用beanName注入,
 *               如果加入@Qualifier("personMapper"),代表使用beanName来进行注入名称是 personMapper
 *               j2ee提供的注解 @Resource 默认beanName找注入,如果BeanName没有找到就自动切换找数据类型相同的注入
 *                  他可以设置2个属性分别是name使用beanName注入(name = "personMapper",),type使用数据类型注入(type = personMapper.class)
 *                  2个属性可以同时使用
 *               @Service说明 此类是一个业务类，需要将此类纳入工厂  等价替换掉 <bean class="xxx.PersonServiceImpl">
 *               @Service默认beanId == 首字母小写的类名"personServiceImpl"
 */
@Service("personService") //自定义beanId为"personService"
@Scope("singleton") //单例模式
//该类中所有的方法,都应用默认事务(什么都不写),可以指定事务隔离级别,传播级别
@Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED,timeout = -1,rollbackFor = Exception.class)
public class PersonServiceImpl implements PersonService {

    @Autowired //注入name=personMapper的bean   使用注释可不写set/get方法,spring特殊处理此类问题
    private PersonMapper personMapper;

    public PersonMapper getPersonMapper() {
        return personMapper;
    }

    public void setPersonMapper(PersonMapper personMapper) {
        this.personMapper = personMapper;
    }

    @Override
    public List<Person> getAll() {
        return personMapper.getAll();
    }

    @Override
    public Person getPersonById(Integer id) {
        return personMapper.getPersonById(id);
    }

    @Override
    public Integer deletePerson(Integer id) throws SQLException {
        int i = 0;
        if (id > 0) {
            //System.out.println(10/0);
            i = personMapper.deletePerson(id);
//            throw new SQLException("sql 异常");
//            int j = 3/0;

        }

//        return personMapper.deletePerson(id);
        return i;
    }

    @Override
    public Integer updatePerson(Person person) {
        return personMapper.updatePerson(person);
    }

    @Override
    public Integer insertPerson(Person person) {
        return personMapper.insertPerson(person);
    }
}
